package org.neurone.userspace.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.neurone.domain.Message;
import org.neurone.domain.alter.MessageAlter;
import org.neurone.domain.persistence.MessagePersistence;
import org.neurone.userspace.dao.MessageDao;
import org.neurone.util.criteria.Criteria;
import org.neurone.util.criteria.CriteriaOperator;
import org.neurone.util.criteria.impl.CriteriaKeyValue;
import org.springframework.orm.hibernate3.HibernateCallback;

public class MessageDaoImpl extends BaseDaoHibernate implements MessageDao {

	@Override
	public void deleteMessage(Long messageID) {
		MessagePersistence mp = find(messageID);
		getHibernateTemplate().delete(mp);
	}

	@Override
	public MessagePersistence find(Long messageID) {
		return (MessagePersistence)getHibernateTemplate().load(MessagePersistence.class, messageID);
	}

	@Override
	public List<Message> findAll(String messageType, int pageSize, int pageIndex) {
		List<Message> list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query = session.createQuery("from MessagePersistence as messagePersistence");
				//query.setFirstResult(pageSize *pageIndex);
				//query.setMaxResults(pageSize);
				List<Message> list = query.list();
				return list;
			}
		});
		
		return list;
	}

	@Override
	public Long newMessage(Message message) {
		MessagePersistence mp = new MessagePersistence();
		mp = fillMessage(message, mp);
		getHibernateTemplate().saveOrUpdate(mp);
		return mp.getMessageID();
	}

	@Override
	public void updateMessage(Message message) {
		MessagePersistence mp = find(message.getMessageID());
		if(mp != null)
		{
			mp = fillMessage(message, mp);
			getHibernateTemplate().update(mp);
		}
	}

	private MessagePersistence fillMessage(Message source, MessagePersistence mp)
	{
		mp.setAvailable(source.isAvailable());
		mp.setFrom(source.getFrom());
		mp.setMessageType(source.getMessageType());
		mp.setPostDate(source.getPostDate());
		mp.setPriority(source.getPriority());
		mp.setStatus(source.getStatus());
		mp.setText(source.getText());
		mp.setTo(source.getTo());
		mp.setTopic(source.getTopic());
		
		return mp;
	}

	@Override
	public List<Message> search(final Criteria terms, int pageSize, int pageIndex) {
		List<Message> list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query = session.createQuery("from MessagePersistence as messagePersistence where "+terms.getSQLCriteria());
				//query.setFirstResult(pageSize *pageIndex);
				//query.setMaxResults(pageSize);
				List<Message> list = query.list();
				return list;
			}
		});

		return list;	
	}
}
